'use strict';

/**
 * @ngdoc function
 * @name passwordAgentHtmlApp.controller:passwordAgentCtrl
 * @description
 * # navCtrl
 * Controller of the passwordAgentApp
 */
angular.module('passwordAgentApp')
  .controller('passwordAgentCtrl', ['$scope', 'alertService', 'passwordService', function ($scope, alertService, passwordService) {
    console.log('passwordAgentCtrl');
    var answers = [
      {ask: "", answer: ""}
    ];
    $scope.answers = answers;

    $scope.addAnswer = function () {
      console.log("add");
      var answer = {};
      answer.ask = "";
      answer.answer = "";

      $scope.answers.push(answer);
    }
    $scope.rememberMe = function () {
      console.log("remember");
      console.log($scope.remember);
    }

    $scope.removeAnswer = function (index) {
      console.log(index);
      //for(var i = 0; i < $scope.answers.length;i++){
      //  $scope.answers[i].answerId == $scope.answerId
      //}
      if ($scope.answers.length <= 1) {
        alertService.add('danger', "最少需要一组问答！");
        return;
      }
      $scope.answers.splice(index, 1);
    }

    $scope.closeAlert = function (index) {
      console.log("closeAlert");
      alertService.closeAlertIdx(index);
    }

    $scope.generateWord = function (){
      console.log($scope);

      var condition = new passwordService.Condition();
      var len = $scope.answers.length;
      condition.answers = [];
      for(var i = 0; i < len; i++){
        var answerVar = new passwordService.Answer();
        answerVar.ask = $scope.answers[i].ask;
        answerVar.answer = $scope.answers[i].answer;
        condition.answers[i] = answerVar;
      }
      condition.email = $scope.email;
      condition.webSite = $scope.site;
      condition.pattern = $scope.pattern;
      if($scope.length != null && $scope.length != ""){
        condition.length = new Number($scope.length);
      }
      $scope.word = passwordService.produceWord(condition);

    }




  }]);
